package com.zhangchuchu.neohealth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangchuchu.neohealth.common.R;
import com.zhangchuchu.neohealth.entity.DTO.RecordsDTO;
import com.zhangchuchu.neohealth.entity.Records;
import com.zhangchuchu.neohealth.service.RecordsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Records)控制层
 *
 * @author makejava
 * @since 2025-04-08 09:47:44
 */
@RestController
@RequestMapping("/records")
public class RecordsController {
    @Resource
    private RecordsService recordsService;

    /**
     * 查询所有数据（不分页）
     */
    @GetMapping("/getList")
    public R<List<Records>> selectList() {
        return R.success(recordsService.list());
    }

    /**
     * 分页查询
     */
    @GetMapping
    public R<Page<Records>> selectAll(Page<Records> page, Records records) {
        return R.success(recordsService.page(page, new QueryWrapper<>(records)));
    }

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("{id}")
    public R<Records> selectOne(@PathVariable Serializable id) {
        return R.success(recordsService.getById(id));
    }

    /**
     * 新增数据
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody Records records) {
        return R.success(recordsService.save(records));
    }

    /**
     * 修改数据
     */
    @PutMapping
    public R<Boolean> update(@RequestBody Records records) {
        return R.success(recordsService.updateById(records));
    }

    /**
     * 删除数据（批量）
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("idList") List<Integer> idList) {
        return R.success(recordsService.removeByIds(idList));
    }

    @GetMapping("/count")
    public R<Integer> countRecordsByMonth(@RequestParam("year") Integer year,@RequestParam("month") Integer month) {
        return R.success(recordsService.countRecordsByMonth(year, month));
    }

    @GetMapping("/content")
    public R<List<RecordsDTO>> getRecordsWithUserProfile(@RequestParam("userId") Integer userId, @RequestParam("year") Integer year, @RequestParam("month") Integer month) {
        return R.success(recordsService.getRecordsWithUserProfile(userId, year, month));
    }
}
